package cn.jji8.QSGUI.Gui;

import cn.jji8.QSGUI.Gui.Goods.Goods;
import cn.jji8.QSGUI.quick.logger.Logger;
import cn.jji8.QSGUI.quick.tool.ConfgTool;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.File;
import java.util.Set;

/**
 * 用于初始化时加载界面
 * */
public class GuiConfigLoad {
    static YamlConfiguration ymlSell;
    static YamlConfiguration ymlBuy;
    public static void load(JavaPlugin main){
        System.out.println(main.getName()+"界面管理器开始加载..");
        main.saveResource("GuiConfigSell.yml",false);
        main.saveResource("GuiConfigBuy.yml",false);
        File fileSell = new File(main.getDataFolder(),"GuiConfigSell.yml");
        File fileBuy = new File(main.getDataFolder(),"GuiConfigBuy.yml");
        ymlSell = YamlConfiguration.loadConfiguration(fileSell);
        ymlBuy = YamlConfiguration.loadConfiguration(fileBuy);
        System.out.println(main.getName()+"界面管理器加载完成..");
    }
    /**
     * 加载gui的出售配置，的配置文件中的配置
     * */
    public static void loadSellConfig(Gui gui){
        Set<String> list = ymlSell.getKeys(false);
        for(String key:list){
            String controller = ConfgTool.getString(ymlSell,key,"控制器","Decorate");
            int x = ConfgTool.getint(ymlSell,key,"x",1);
            int y = ConfgTool.getint(ymlSell,key,"y",1);
            Class c;
            try {
                c = Class.forName("cn.jji8.QSGUI.Gui.Goods."+controller);
            } catch (ClassNotFoundException e) {
                try {
                    c = Class.forName(controller);
                } catch (ClassNotFoundException s) {
                    e.printStackTrace();
                    s.printStackTrace();
                    Logger.putSevere(ymlSell.getName()+"中，"+key+"的控制器属性"+controller+"错误");
                    continue;
                }
            }
            try {
                Object o = c.newInstance();
                Goods goods = (Goods) o;
                goods.setGoods(gui.shop,ymlSell,key);
                gui.addGoods(x,y,goods);
            } catch (InstantiationException e) {
                e.printStackTrace();
                Logger.putSevere(ymlSell.getName()+"中，"+key+"的控制器属性"+controller+"实例化错误");
            } catch (IllegalAccessException e) {
                e.printStackTrace();
                Logger.putSevere(ymlSell.getName()+"中，"+key+"的控制器属性"+controller+"非法访问");
            }

        }
    }
    /**
     * 加载gui的收购配置，的配置文件中的配置
     * */
    public static void loadBuyConfig(Gui gui){
        Set<String> list = ymlBuy.getKeys(false);
        for(String key:list){
            String controller = ConfgTool.getString(ymlBuy,key,"控制器","Decorate");
            int x = ConfgTool.getint(ymlBuy,key,"x",1);
            int y = ConfgTool.getint(ymlBuy,key,"y",1);
            Class c;
            try {
                c = Class.forName("cn.jji8.QSGUI.Gui.Goods."+controller);
            } catch (ClassNotFoundException e) {
                try {
                    c = Class.forName(controller);
                } catch (ClassNotFoundException s) {
                    e.printStackTrace();
                    s.printStackTrace();
                    Logger.putSevere(ymlBuy.getName()+"中，"+key+"的控制器属性"+controller+"错误");
                    continue;
                }
            }
            try {
                Object o = c.newInstance();
                Goods goods = (Goods) o;
                goods.setGoods(gui.shop,ymlBuy,key);
                gui.addGoods(x,y,goods);
            } catch (InstantiationException e) {
                e.printStackTrace();
                Logger.putSevere(ymlBuy.getName()+"中，"+key+"的控制器属性"+controller+"实例化错误");
            } catch (IllegalAccessException e) {
                e.printStackTrace();
                Logger.putSevere(ymlBuy.getName()+"中，"+key+"的控制器属性"+controller+"非法访问");
            }

        }
    }
}
